using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using OpenQuarters.WebQuarters.Utils;
using System.Text;
using OpenQuarters.WebQuarters.Core;

namespace OpenQuarters.WebQuarters.Core.Web.Areas.CMS.Controllers
{
    public class AdminController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ApproveWorkflow(string id)
        {
            // id is WorkflowItemId
            var resp = new JFormResponse<WorkflowItem>();
            var workflow = WorkflowItem.Load(Convert.ToInt64(id));

            if (!workflow.json_CurrentUserHasPermission)
            {
                resp.errorMessages[""].Add("You are not authorised to approve this workflow item.");
            }
            else
            {
                new WorkflowComment()
                {
                    WorkflowItemId = workflow.WorkflowItemId,
                    UserId = Core.User.Current.UserId,
                    Comment = "Workflow item '" + workflow.ToString() + "' has been approved",
                }.Save();

                if (workflow.UserId != Core.User.Current.UserId)
                {
                    new WorkflowComment()
                    {
                        WorkflowItemId = workflow.WorkflowItemId,
                        UserId = Core.User.Current.UserId,
                        Comment = "Workflow item '" + workflow.ToString() + "' has been approved",
                    }.Save();
                }

                workflow.Approve();
                resp.result = null;
            }

            ViewData.Model = resp.ToJSON();
            return View(Site.Helpers.SharedView.Service);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RejectWorkflow(string id)
        {
            // id is WorkflowItemId
            var resp = new JFormResponse<WorkflowItem>();
            var workflow = WorkflowItem.Load(Convert.ToInt64(id));

            if (!workflow.json_CurrentUserHasPermission)
            {
                resp.errorMessages[""].Add("You are not authorised to reject this workflow item.");
            }
            else
            {
                new WorkflowComment()
                {
                    WorkflowItemId = workflow.WorkflowItemId,
                    UserId = Core.User.Current.UserId,
                    Comment = "Workflow item '" + workflow.ToString() + "' has been rejected",
                }.Save();

                if (workflow.UserId != Core.User.Current.UserId)
                {
                    new WorkflowComment()
                    {
                        WorkflowItemId = workflow.WorkflowItemId,
                        UserId = Core.User.Current.UserId,
                        Comment = "Workflow item '" + workflow.ToString() + "' has been rejected",
                    }.Save();
                }

                workflow.Reject();
                resp.result = null;
            }

            ViewData.Model = resp.ToJSON();
            return View(Site.Helpers.SharedView.Service);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddWorkflowComment()
        {
            var resp = new JFormResponse<WorkflowComment>();

            if (!Core.User.Current.HasPermission(Permissions.Admin.AddWorkflowComment))
            {
                resp.errorMessages[""].Add("You are not authorised to add workflow comments.");
            }
            else
            {
                var cmt = new WorkflowComment()
                {
                    Comment = Request.Form["comment"].StripHTML(),
                    UserId = Core.User.Current.UserId,
                    WorkflowItemId = Convert.ToInt64(Request.Form["id"])
                }.Save();
                resp.result = cmt;
            }

            ViewData.Model = resp.ToJSON();
            return View(Site.Helpers.SharedView.Service);
        }
    }
}